package com.sti.framework.models.User.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sti.framework.core.service.BaseService;
import com.sti.framework.core.web.Page;
import com.sti.framework.core.web.SimplePage;
import com.sti.framework.models.User.data.*;
import com.sti.framework.models.User.mapper.RoleAuthorityMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class RoleAuthorityService extends BaseService<RoleAuthorityMapper, RoleAuthority> {

    public SimplePage<RoleAuthority> getAll(Page page, Long role_id) {

        QueryWrapper<RoleAuthority> wrapper = new QueryWrapper();

        wrapper.eq("role_id",role_id);
        wrapper.eq("access_level",1);

        Page<RoleAuthority> list = super.getBaseMapper().selectPage(page, wrapper);
        return new SimplePage(list.getRecords(), list.getTotal(),list.getCurrent(),list.getSize());
    }

    public List<RoleAuthority> getAll(Long id) {

        QueryWrapper<RoleAuthority> wrapper = new QueryWrapper();

        wrapper.eq("role_id",id);
        wrapper.eq("access_level",1);

        return super.getList(wrapper);
    }


    public RoleAuthority getForRole(Long role_id, String tag) {

        QueryWrapper<RoleAuthority> wrapper = new QueryWrapper();

        wrapper.eq("role_id",role_id);
        wrapper.eq("tag",tag);

        return super.getOne(wrapper);
    }

    @Transactional(value = "transactionManager")
    public void deleteForRole(Long id) {

        QueryWrapper<RoleAuthority> wrapper = new QueryWrapper();

        wrapper.eq("role_id",id);

        super.remove(wrapper);
    }
}
